<?php
namespace app\admin\controller;

use think\controller;
use think\Request;
use app\admin\model\AdminRole as AdminRoleModel;

class AdminRole extends Base
{
    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new AdminRoleModel();
    }

    public function index()
    {
        $data = $this->model->field('id,name,permissions,create_time,status')->select();

        $this->assign("data", $data);
        return $this->fetch();
    }

    public function addOrEdit($id = 0)
    {
        if ($id != 0) {
            $data = $this->model->find($id);
            // 已有权限数组
            $role_powers = explode(',', $data->permissions);
        } else {
            $data = [
                'id' => 0,
                'name' => '',
                'permissions' => '',
                'note' =>''
            ];
            $role_powers = [];
        }
        // 所有可用权限
        $data_permission = model('admin_menu')->field('id, pid, name')->where('status', 1)->select();
        
        $data_permission = obj_tree($data_permission);
        
        $this->assign('data_permission', $data_permission);
        $this->assign('role_powers', $role_powers);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function doAddOrEdit(Request $request = null, $id = 0)
    {
        if ($request->isPost()) {
            $param = $request->param();

            if ($param['name'] == '') {
                $this->error("目录名不能为空");
            }
            $permissions = '';
            
            if (!empty($param['perm_check'])) {
                $perm_checks = $param['perm_check'];
                $permissions = implode(',', $perm_checks);
            }
            try {
                if ($param['id'] != 0) {
                    $this->model->save([
                        'name' => $param['name'],
                        'permissions' => $permissions,
                        'note' => $param['note']
                    ], ['id' => $param['id']]);
                } else {
                    $this->model->save([
                        'name' => $param['name'],
                        'permissions' => $permissions,
                        'note' => $param['note']
                    ]);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('操作成功', 'index');
        }
    }

    public function delete($id = null)
    {
        if (Request::instance()->isAjax()) {
            $U_role_id = model('admin_user')->where('id', $this->uid)->value('role_id');

            if (is_array($id)) {
                if (in_array('1', $id)) {
                    return ['status'=>0,'msg'=>'超级管理员无法删除'];
                }
                if (in_array($U_role_id, $id)) {
                    return ['status'=>0,'msg'=>'当前登录用户角色无法删除'];
                }
            } else {
                if ($id == 1) {
                    return ['status'=>0,'msg'=>'超级管理员无法删除'];
                }
                if ($id == $U_role_id) {
                    return ['status'=>0,'msg'=>'当前登录用户角色无法删除'];
                }
            }
            if ($this->model->destroy($id)) {
                return ['code' => 1,'msg'=>'操作成功'];
            } else {
                return ['code' => 0,'msg'=>'操作失败'];
            }
        }
    }

    public function status($id, $status = 0)
    {
        if (Request::instance()->isAjax()) {
            if ($this->model->save(['status'=>$status], ['id' => $id])) {
                return ['code'=>1, 'msg'=>'操作成功'];
            } else {
                return ['code'=>0,'msg'=>'操作失败'];
            }
        }
    }
}
